﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using System.Collections;

namespace Gui.Functies
{
    public partial class Fileshare : UserControl
    {
        int projectID;
        string selected;
        private MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
        Hashtable fileinfo = new Hashtable();

        public Fileshare()
        {
            
        }

        public Fileshare(int projectnummer)
        {
            projectID = projectnummer;
            InitializeComponent();
            getfiles();
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            Form upload = new Upload(projectID);
            upload.Show();
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            selected = listViewFileOverzicht.SelectedItems[0].Text;
            
            Form download = new Download(projectID, selected);
            download.Show();
        }

        private void getfiles()
        {
            try
            {
                grabConnection.Open();
                MySqlCommand filesGet = new MySqlCommand("SELECT DocName, Entrydate FROM doc_upload WHERE ProjectID='"+projectID+"'", grabConnection);

                MySqlDataReader reader = filesGet.ExecuteReader();

                while (reader.Read())
                {
                    fileinfo.Add(reader["Docname"], reader["Entrydate"]);
                }
                reader.Dispose();
                filesGet.Dispose();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }

            grabConnection.Close();
            showfiles();
        }

        private void showfiles()
        {
            foreach (string key in fileinfo.Keys)
            {
                ListViewItem item = new ListViewItem(String.Format("{0}", key));
                item.SubItems.Add(String.Format("{0}", fileinfo[key]));
                listViewFileOverzicht.Items.Add(item);
            }
        }
    }
}
